/*
 * JaCoLiB
 * Copyright (C) 2008 Alessandro Serra
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package it.gashale.jacolib.main.configurator;

import it.gashale.jacolib.core.JacolibError;

import java.util.logging.Logger;

public abstract class AbstractEnvironment implements Environment {
	private static Logger logger = Logger.getLogger(AbstractEnvironment.class.getName());
	
	protected AbstractEnvironment() throws JacolibError {	
	}
	
	protected String getDotName(String prefixes[], int pos, String name[]) {
		String fullname="";
		for(int i=0; i<(prefixes.length-pos); ++i) {
			String prefix=prefixes[i].toLowerCase();
			if(i==0) fullname+=prefix;
			else fullname+="."+prefix;
		}
		for(int i=0; i<(name.length); ++i) {
			String nam=name[i].toLowerCase();
			if(prefixes.length==0 && i==0) fullname+=nam;
			else fullname+="."+nam;
		}
		return fullname;
	}
	protected String getEnvName(String prefixes[], int pos, String name[]) {
		String fullname="";
		for(int i=0; i<(prefixes.length-pos); ++i) {
			String prefix=prefixes[i].toUpperCase();
			if(i==0) fullname+=prefix;
			else fullname+="_"+prefix;
		}
		for(int i=0; i<(name.length); ++i) {
			String nam=name[i].toUpperCase();
			if(prefixes.length==0 && i==0) fullname+=nam;
			else fullname+="_"+nam;
		}
		return fullname;
	}
	protected String getPropName(String prefixes[], int pos, String name[]) {
		String fullname="";
		for(int i=0; i<(prefixes.length-pos); ++i) {
			String prefix=prefixes[i];
			if(i==0) fullname+=prefix;
			else fullname+="."+prefix;
		}
		if(prefixes.length>0) fullname+=".";
		for(int i=0; i<name.length; ++i) {
			String nam=name[i];
			fullname+=nam;
		}
		return fullname;
	}
	
	public String getValue(String name[], String default_value) throws JacolibError {
		return getValue(new String[0], name, default_value);
	}
	public String getValue(String prefixes[], String name[], String default_value) throws JacolibError {
		//logger.finest("looking for "+getDotName(prefixes,0,name)+", "+getPropName(prefixes,0,name)+", or "+getEnvName(prefixes,0,name)+" variables.");
		String value=null;
		for(int pos=0; pos<=prefixes.length; ++pos) {
			value=getSystemProperty(getDotName(prefixes,pos,name));
			if(value!=null) break;
			value=getProperty(getPropName(prefixes,pos,name));
			if(value!=null) break;
			value=getEnv(getEnvName(prefixes,pos,name));
		}
		if(value==null && default_value!=null) {
				value=default_value;
		}
		
		if(value==null) {
				throw new JacolibError("Impossible to load "+getDotName(prefixes,0,name)+", "+getPropName(prefixes,0,name)+", or "+getEnvName(prefixes,0,name)+" variable.");	
		}
		logger.finest("variable "+getDotName(prefixes,0,name)+", "+getPropName(prefixes,0,name)+", or "+getEnvName(prefixes,0,name)+" equals to "+value+".");
		return value;
	}
	
	protected abstract String getProperty(String name);
	protected abstract String getSystemProperty(String name);
	protected abstract String getEnv(String name);
}
